#pragma once 

/*
	weapon should search for a target if it has no current target 
	weapon should fire on update given fully reloaded and lined up

	todo make a weapon that fires at a given angle, ex: 15deg
	compute the proper v needed to hit the target: d = (v^2*sin(2*angle))/g
		- Extension: Assume target velocity constant, compute time of flight, aim for position of object given constant velocity over time of flight 
	alternatively make a weapon with constant v but variable angle, see wiki for equ
	 
*/

class PlayerObj;
class Terrain;

#include "core/Collision.hpp"

class CannonBall : public Obj {
public:
	CannonBall(glm::vec3 & pos, const glm::vec3 & v, float radius, PlayerObj* target) 
		: Obj(pos), v(v), target(target), lifeTime(0.0f), radius(radius), sphereGeom(pos, radius), particleCounter(0.0f), particleRate(100.0f)
	{
		type = type_cannonBall;
	}
	virtual ~CannonBall() {}

	void update(float dt);
	void draw(bool picking);
	virtual CollGeom* getCollGeom();

	virtual void handleColl(Obj* other);
	virtual void handleColl(PlayerObj* other);
	virtual void handleColl(CannonBall* other);
	virtual void handleColl(Terrain* other);

	CollisionSphere sphereGeom;
	glm::vec3 v;
	PlayerObj* target;
	float lifeTime;
	float radius;

	float particleRate;
	float particleCounter;
};

class Weapon : public Obj {

public:
	Weapon(Player* owner, const glm::vec3 & pos, const glm::vec3 & relPos, const glm::vec3 & dim);

	virtual void handleColl(Obj* other) { throw std::runtime_error("Collision handling in weapon called\n"); }
	virtual void handleColl(PlayerObj* other) { throw std::runtime_error("Collision handling in weapon called\n"); }
	virtual void handleColl(CannonBall* other) { throw std::runtime_error("Collision handling in weapon called\n"); }

	void update(float dt);
	void draw(bool picking);
	void move(const glm::vec3& pos);
	void assignTarget(Obj* target);
	virtual CollGeom* getCollGeom();

private:
	Player* owner;
	PlayerObj* target;
	std::vector<CannonBall*> projectiles;
	float reload;
	float reloadTime;
	float radius;
	glm::vec3 dim;
	glm::vec3 relPos;
};

